package org.edu.modules.common.controller;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.edu.common.api.vo.Result;
import org.edu.common.dto.ExcelPo;
import org.edu.common.dto.ImportPo;
import org.edu.common.system.base.service.BaseService;
import org.edu.common.util.SpringContextUtils;
import org.edu.common.utils.PoiUtils;
import org.edu.modules.common.service.IVeBaseBanJiService;
import org.edu.modules.common.service.IVeBaseTeacherService;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Api(tags = {"基础的文件导入"})
@RestController
@RequestMapping("/veScheduleExcel")
@Slf4j
public class VeCommonExcel {

    @Resource
    private IVeBaseTeacherService iVeBaseTeacherService;
    @Resource
    private IVeBaseBanJiService iVeBaseBanJiService;



    @ApiOperation("导出数据")
    @PostMapping("/exportTemp")
    public Result<?> export(@RequestBody ImportPo importPo, HttpServletResponse resp) {
        List<ExcelPo> list = new ArrayList<>();
        for (String page1 : importPo.getTemp()) {
            list.add(new ExcelPo(page1, "name"));
        }
        PoiUtils.downloadFile(resp, new ArrayList<>(), list, importPo.getTempMsg());
        return Result.OK();
    }


    @ApiOperation("文件导入")
    @RequestMapping("/importExcel")
    public Result<?> importExcel(@RequestParam(value = "file", required = false) MultipartFile file,
                                 String implName, HttpServletRequest request) {
        if (implName == null || !implName.contains(".")) {
            return Result.error("接口未配置");
        }
        String[] arr = implName.split("\\.");

        try {
            ImportParams params = new ImportParams();
            params.setTitleRows(2);
            params.setHeadRows(1);
            params.setNeedSave(true);

            ExcelReader excelReader = ExcelUtil.getReader(file.getInputStream());
            List<Map<String, Object>> list = excelReader.readAll();
            if (arr[0].equals("VeBaseBanJiServiceImpl")) {
                int code=iVeBaseBanJiService.importExcel(list);
                if (code==1){
                    return Result.error("所属年级错误");
                }else if(code==2){
                    return Result.error("专业名称错误");
                }else if(code==3){
                    return Result.error("所属班主任错误");
                }else if(code==4){
                    return Result.error("班长名称错误");
                }else if(code==5){
                    return  Result.error("教室不存在");
                }

            }else if ("veBaseTeacherServiceImpl".equals(arr[0])){
                int code=iVeBaseTeacherService.importExcel(list);
                if (code==1){
                    return Result.error("专业组不存在");
                }else if(code==2){
                    return Result.error("部门名称不存在");
                }
            }
//            BaseService bean = SpringContextUtils.getBean(arr[0], BaseService.class);
//            bean.importExcel(list);
           /* Method method = bean.getClass().getMethod(arr[1],List.class);
            method.invoke(list);*/
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
        return Result.OK();
    }


}
